

public class binarysearch {
    public static void main(String[] args) {
        int [] array = {1,3,4,5,6,7,8,9,10};
        System.out.println("search1 result = " + search1(array, 2));
        System.out.println("search1 result = " + search1(array, 3));
        System.out.println("search1 result = " + search1(array, 9));

        System.out.println("search2 result = " + search2(array, 0, array.length - 1, 2));
        System.out.println("search2 result = " + search2(array, 0, array.length - 1, 3));
        System.out.println("search2 result = " + search2(array, 0, array.length - 1, 9));

    }
    static int search2(int[] array, int low, int high, int target) {
        if (low > high)
            return -1;
        int mid = (low + high)/2;
        int guess = array[mid];
        if( guess == target) {
            return target;
        }
        else if (target > guess) {
            return search2(array, mid + 1, high, target);
        } else {
            return search2(array, low, mid - 1, target);
        }

    }

    static int search1(int[] array, int target) {
        
        int low = 0;
        int high = array.length - 1;

        while (low <= high) {
            int mid = (low + high)/2;
            int guess = array[mid];
            if (guess == target) {
                return guess;
            } else if (target > guess) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}